package edu.stanford.bmir.protege.web.client.ui.ontology.transform;

import java.util.Comparator;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.Connection;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Radio;
import com.gwtext.client.widgets.layout.VerticalLayout;
import com.gwtext.client.widgets.tree.AsyncTreeNode;
import com.gwtext.client.widgets.tree.MultiSelectionModel;
import com.gwtext.client.widgets.tree.TreeNode;
import com.gwtext.client.widgets.tree.TreePanel;
import com.gwtext.client.widgets.tree.XMLTreeLoader;

public class SelectTree extends Composite {
	
	SelectMapping selectMapping;
	
	private VerticalPanel verPanel = new VerticalPanel();
	
	public SelectTree ( final SelectMapping sm ) {
		initWidget( verPanel );
		
		this.selectMapping = sm;
  
		 final TreePanel treePanel = new TreePanel();  
	        treePanel.setTitle("本体元素选择");  
	        treePanel.setAnimate(true);  
	        treePanel.setEnableDD(true);  
	        treePanel.setContainerScroll(true);  
	        treePanel.setRootVisible(true);  
	        treePanel.setWidth( 300 );
	        treePanel.setSelectionModel(new MultiSelectionModel());  
	        treePanel.setHeight(360);  
	  
	        final XMLTreeLoader loader = new XMLTreeLoader();  
	        loader.setDataUrl("countries-cb.xml");  
	        loader.setMethod(Connection.GET);  
	        loader.setRootTag("countries");  
	        loader.setFolderTitleMapping("@title");  
	        loader.setFolderTag("team");  
	        loader.setLeafTitleMapping("@title");  
	        loader.setLeafTag("country");  
	        loader.setQtipMapping("@qtip");  
	        loader.setDisabledMapping("@disabled");  
	        loader.setCheckedMapping("@checked");  
	        loader.setIconMapping("@icon");  
	        loader.setAttributeMappings(new String[]{"@rank"});  
	  
	        AsyncTreeNode root = new AsyncTreeNode("元素", loader);  
	        treePanel.setRootNode(root);  
	  
	        root.expand();  
	        treePanel.expandAll();  
	        root.select();  
	  
	  
	        Button selectedButton = new Button("确定", new ButtonListenerAdapter() {  
	            public void onClick(Button button, EventObject e) {  
	                MultiSelectionModel selectionModel = (MultiSelectionModel) treePanel.getSelectionModel();  
	                TreeNode[] nodes = selectionModel.getSelectedNodes();  
	                String nodesString = "";  
	                for (int i = 0; i < nodes.length; i++) {  
	                    TreeNode node = nodes[i];  
	                    nodesString += "<br>" + node.getText();  
	                }  
	                System.out.println("Selected Nodes :" + nodesString);  
	                selectMapping.closeWindow( nodesString );
	            }  
	        }); 
	  
	        Panel verticalPanel = new Panel();  
	        verticalPanel.setLayout(new VerticalLayout(15));  
	        verticalPanel.add(treePanel);  
	        verticalPanel.add(selectedButton);  
	        verPanel.add(verticalPanel);  
	    }  
	}  